package com.atguigu.compress;

import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @description: xxx
 * @time: 2020-01-02 16:35
 * @author: baojinlong
 **/
public class TestCompressExample {
    public static void main(String[] args) throws Exception {
        // D:/home/test/big-data/input/compress/compress.txt
        // org.apache.hadoop.io.compress.GzipCodec
        // org.apache.hadoop.io.compress.DefaultCodec
        //compressMethod("E:/home/test/big-table/input/compress/compress.txt", "org.apache.hadoop.io.compress.BZip2Codec");
        deCompressMethod("E:/home/test/big-table/input/compress/compress.txt.bz2");
    }

    @SuppressWarnings("resource")
    private static void deCompressMethod(String fileName) throws IOException {
        CompressionCodecFactory factory = new CompressionCodecFactory(new Configuration());
        CompressionCodec codec = factory.getCodec(new Path(fileName));
        if (codec == null) {
            System.out.println("当前系统不支持解压缩格式,codec=" + codec);
            return;
        }
        // 1.获取输入流
        CompressionInputStream cis = codec.createInputStream(new FileInputStream(new File(fileName)));
        // 2.获取输出流
        FileOutputStream fos = new FileOutputStream(new File(fileName + ".decode"));
        // 3.流的对拷
        IOUtils.copyBytes(cis, fos, 1024 * 1024, false);
        // 4.关闭资源
        fos.close();
        cis.close();
        System.out.println("解压结束");
    }

    private static void compressMethod(String fileName, String className) throws Exception {
        // 1.获取输入流
        FileInputStream fileInputStream = new FileInputStream(new File(fileName));
        // 2.获取输出流
        Class<?> codeClass = Class.forName(className);
        CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codeClass, new Configuration());
        FileOutputStream fileOutputStream = new FileOutputStream(new File(fileName + codec.getDefaultExtension()));
        CompressionOutputStream outputStreamCompress = codec.createOutputStream(fileOutputStream);
        // 3.流的对拷
        IOUtils.copyBytes(fileInputStream, outputStreamCompress, 1024 * 1024, false);
        // 4.关闭资源
        outputStreamCompress.close();
        fileOutputStream.close();
        fileInputStream.close();
        System.out.println("程序执行结束");
    }

    /**
     * @param hotPointList  热点文章
     * @param recommendList 推荐文章
     * @return
     */
    private static List<Map<String, Object>> getResultList(List<Map<String, Object>> hotPointList, List<Map<String, Object>> recommendList) {
        List<Map<String, Object>> returnList = Lists.newArrayList();
        // 取热点的第一条数据
        returnList.add(hotPointList.get(0));
        // 取推荐的第一条
        returnList.add(recommendList.get(0));
        // 剩下的集合依次放入
        List<Map<String, Object>> shuffleList = Lists.newArrayList();
        for (int i = 1, size = hotPointList.size(); i < size; i++) {
            // 从第二条数据开始
            shuffleList.add(hotPointList.get(i));
        }
        for (int i = 1, size = recommendList.size(); i < size; i++) {
            // 从第二条数据开始
            shuffleList.add(recommendList.get(i));
        }
        // 将集合打乱
        Collections.shuffle(shuffleList);
        // 添加到最终结果中
        returnList.addAll(shuffleList);
        return returnList;
    }
}
